package com.codechuan.hallo_internal.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.codechuan.hallo_internal.MyApplication;
import com.codechuan.hallo_internal.R;
import com.codechuan.hallo_internal.common.Constant;
import com.codechuan.hallo_internal.entity.CityInfo;
import com.codechuan.hallo_internal.ui.home.HomeFragment;
import com.codechuan.hallo_internal.utils.ContextUtil;
import com.codechuan.hallo_internal.utils.DialogUtil;
import com.codechuan.hallo_internal.utils.MyProjectHttpUtil;
import com.example.libbaiduselectlocation.BDLocationActivity;
import com.example.libbaiduselectlocation.bean.SelectLocationBDBean;
import com.google.android.material.bottomnavigation.BottomNavigationView;

/**
 * @Deprecated fragment载体Activity
 */
public class LoginActivity extends BDLocationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);

        // table切换启动
        BottomNavigationView navView = findViewById(R.id.nav_view);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupWithNavController(navView, navController);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == Activity.RESULT_OK) {
            SelectLocationBDBean bean = (SelectLocationBDBean) data.getSerializableExtra("data");
            String district = bean.getLocation().getDistrict();
            String address = bean.getLocation().getAddress();

            Constant.startAddress = district + address;
            HomeFragment.updateStartAddress(Constant.startAddress);
            // 开始定位信息
            new Thread(() -> {
                Constant.cityInfo = MyProjectHttpUtil.getCityInfo(Constant.startAddress);
                if (Constant.cityInfo != null) {
                    Constant.handler.post(() -> DialogUtil.showDialog(ContextUtil.context, "定位成功."));
                } else {
                    Constant.handler.post(() -> DialogUtil.showDialog(ContextUtil.context, "定位失败."));
                }
            }).start();
        }
    }

    @Override
    public void onLocation(String province, String city, String area, String featureName, String addressLine){
           if (featureName == null || featureName.isEmpty()){
               Constant.handler.post(()->{
                   Constant.startAddress = "请手动定位~";
                   Constant.SS_DW_ADDRESS = "请手动定位~";
                   HomeFragment.updateStartAddress("请手动定位~");
               });
               return;
           }
        Constant.startAddress = featureName.substring(2);
        Constant.SS_DW_ADDRESS =  featureName.substring(2);
        //HomeFragment.updateStartAddress(featureName.substring(2));

    }


    @Override
    public void  onLocationFail(String message) {
        Log.e("location",message);
    }
}